<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="fish" /><span>京海市强盛集团有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 主体内容的布局
        :data 指定要渲染的数据
        :props 指定数据中的对象的某个属性在树形组件显示及子级节点的属性名
        @node-click: 点击事件  点击某一个点, 获取节点对象数据 -->
        <el-tree
          :data="list"
          :props="{ children: 'children', label: 'name' }"
        >
          <!-- 使用插件在el-tree中自定义内容 -->
          <!-- 在template上使用默认作用域插槽, #default={data} 获取组件上的数据进行渲染 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <!-- 使用v-if判断部门有没有子部门,从而优化删除部门功能  当有子部门时,不提供删除功能 -->
                        <el-dropdown-item v-if="data.children && data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="isEdit ? '编辑部门': '新增子部门'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hClose"
      >
        <!-- 父传子:id="curId"  :is-edit="isEdit" 子传父 @success="hSuccess" -->
        <!-- 解决数据不更新的方式
        1. v-if="showDialog" 打开弹层创建组件, 关闭弹层销毁组件
        2. 在父组件中调用子组件中的方法
        缺点: 会影响性能 -->
        <addOrEdit
          :id="curId"
          ref="myDept"
          :origin-list="originList"
          :is-edit="isEdit"
          @close="showDialog=false"
          @success="hSuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { tranListToTreeList } from '@/utils'
import addOrEdit from './deptDialog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      originList: [],
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false // 判断是添加还是编辑
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 关闭弹层 清空表单
    hClose() {
      this.$refs.myDept.resetForm()
    },
    hSuccess() {
      this.showDialog = false
      this.loadDepartments()
    },
    // 添加
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    // 编辑
    hEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
      this.$nextTick(() => {
        this.$refs.myDept.loadDepartmentsDetail()
      })
    },
    // 删除
    hDel(id) {
      this.$confirm('你确定要删除该部门吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartments(id)
        console.log(res, '删除部门')
        // if (res.success) {
        //   this.loadDepartments()
        // }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async loadDepartments() {
      try {
        const { data } = await getDepartments()
        // console.log(data.data.depts, 'res')
        this.originList = data.data.depts.map(item => {
          const { name, code, id, pid } = item
          return { name, code, id, pid }
        })
        console.log(this.originList, 'originList')
        data.data.depts.shift() // 删除数组中第一条数据--公司的名称
        this.list = tranListToTreeList(data.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>
</style>
